# _*_ encoding:utf-8 _*_
from random import choice

from django_redis import get_redis_connection
from django.conf import settings
from time import sleep
from games.models import UserRecord, PlatformRecord
from rest_framework.response import Response
from rest_framework.views import APIView
from rest_framework.viewsets import ViewSetMixin
from utils.auth import LuffyAuth
from utils.redisflie import redisfile
from utils.timer import Plattime


class UserRecordView(ViewSetMixin,APIView):
    conn = get_redis_connection("default")

    # authentication_classes = [LuffyAuth,]

    def creates(self,request,*args,**kwargs):
        '''创建用户记录'''
        token = request.data.get('token')
        small_big = request.data.get('small_big')
        beton = request.data.get('beton')
        # nper  = request.data.get('nper')
        count = request.data.get('count')

        nper = Plattime().timers()
        print(nper,type(nper))

        ret = {'code': 1000, 'data': None}
        #把金钱以及猜中的大小放入Redis数据库
        # nper_redis = int(nper.split('-')[-1])
        beton_key = settings.SHOPPING_CAR_KEY % (nper, int(token))  # "beton_%s_%s"
        beton_dict = {
            'beton_key': beton,
            'small_big': small_big
        }
        self.conn.hmset(beton_key, beton_dict)

        if count<beton:
            ret['code'] = 1001
            ret['error'] = '账户余额不足'

        else:
           UserRecord.objects.create(token=token, small_big=small_big, count=count, beton=beton,nper=nper)


        return Response(ret)

    def update(self, request, *args, **kwargs):
        '''更新用户记录及加入平台记录'''

        ret = {'code': 1000, 'data': None}
        # token = request.data.get("token")

        # 在Redis数据库中取出数据的和做比较
        sumbig = sum(redisfile().redises()[0])
        sumsamll = sum(redisfile().redises()[1])
        print(sumbig, sumsamll, type(sumsamll))

        # dates = UserRecord.objects.all().filter(token=token).order_by('-add_time').first()  需要再次传来token值  把下面所有token值 删除

        dates = UserRecord.objects.all().order_by('-add_time').first()
        token = dates.token
        count = dates.count
        small_big = dates.small_big
        print(small_big)
        beton = dates.beton
        if sumbig >= sumsamll:
            if small_big == 'big':
                counts = count - beton
                UserRecord.objects.filter(token=token, small_big="big").update(iswining="los", count=counts,payoff=-beton,status="over")
            else:
                counts = count + beton
                UserRecord.objects.filter(token=token, small_big="small").update(iswining="win", count=counts,payoff=beton,status="over")
        else:
            if small_big == 'small':
                counts = count - beton
                UserRecord.objects.filter(token=token, small_big="small").update(iswining="los", count=counts,payoff=-beton,status="over")
            else:
                counts = count + beton
                UserRecord.objects.filter(token=token, small_big="big").update(iswining="win", count=counts,payoff=beton, status="over")

        userrecord_id = UserRecord.objects.all().order_by("-add_time").filter()[0]
        userrecord_id = userrecord_id.id
        if sumbig >= sumsamll:


            seeds = "01234"
            num_str = int(choice(seeds))
            isbigs = "small"
            PlatformRecord.objects.create(num=num_str, isbigs=isbigs,userrecord_id=userrecord_id)

        else:
            seeds = "56789"
            num_str = int(choice(seeds))
            isbigs = "big"
            PlatformRecord.objects.create(num=num_str, isbigs=isbigs,userrecord_id=userrecord_id)

        return Response(ret)